package com.smcaiot.jfttask.domain;

import com.smcaiot.jfttask.config.TestMybatisPlusConfig;
import com.smcaiot.jfttask.config.TestSpringConfig;
import com.smcaiot.jfttask.south.port.TaskRepository;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author: cz
 * @Date: 2023/10/7
 * @Description:
 */
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
//@PowerMockIgnore( {"javax.management.*", "javax.net.*"})
@PrepareForTest(TaskId.class)
@ContextConfiguration(classes = {TestMybatisPlusConfig.class, TestSpringConfig.class})
public class TaskRepositoryIT {
    @Autowired
    private TaskRepository taskRepository;
    private TaskId taskId = TaskId.from("34534-4353-ghfd-xexred3-34d");

    @Before
    public void setUp() throws Exception {
        PowerMockito.mockStatic(TaskId.class);
        PowerMockito.when(TaskId.next()).thenReturn(taskId);
        PowerMockito.when(TaskId.from(Mockito.anyString())).thenReturn(taskId);

        taskRepository.deleteByTaskId(taskId);
    }

    private Task createTask() {
        String targetDepartment = "420185000000";
        String ccId = "M023536";
        Task task = new Task(taskId, targetDepartment, null, ccId);
        task.setType("1");
        task.setTaskName("领域集成测试任务1");
        return task;
    }

    @Test
    public void should_find_task_after_save_task() {
        //given
        Task task = createTask();
        //when
        taskRepository.saveTask(task);
        //then
        Task findTask = taskRepository.findByTaskId(taskId);
        Assertions.assertThat(findTask).isNotNull();
        Assertions.assertThat(findTask.getTaskId()).isEqualTo(taskId);
    }
}
